/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.action;

import com.myapp.dbdao.ReportDBDAO;
import com.myapp.struts.bean.EstimateBean;
import com.myapp.struts.form.Estimate5bForm;
import com.myapp.util.CommonUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author toint
 */
public class Report5bAction extends Action {

    @Override
    public ActionForward execute (ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse res) throws Exception {
        Estimate5bForm frm = (Estimate5bForm) form;
        Map beans = new HashMap();
        String templateFilename = "/jsp/template/BaoCao5B.xls";
        String reportFilename = "/report_out/BaoCao5B_" + CommonUtils.autoNumber() + ".xls";
        List<EstimateBean> lstBean = ReportDBDAO.getDataListEstimate5b(frm);
        List<EstimateBean> lstEstimate = new ArrayList<EstimateBean>();
        int tt = 0;
        for (int i = 0; i < lstBean.size(); i++) {
            EstimateBean ibean = lstBean.get(i);
            if (ibean.getParentId() == 0) {
                ibean.setMatt(EstimateBean.arrCode[tt]);
                lstEstimate.add(ibean);
                int sott = 1;
                for (int j = 0; j < lstBean.size(); j++) {
                    EstimateBean jbean = lstBean.get(j);
                    if (jbean.getParentId() != 0 && jbean.getParentId() == ibean.getWantId()) {
                        jbean.setSott(sott);
                        sott++;
                        lstEstimate.add(jbean);
                    }
                }
                tt++;
            }
        }
        beans.put("lstEstimate", lstEstimate);
        XLSTransformer transformer = new XLSTransformer();
        if (templateFilename != null && reportFilename != null) {
            transformer.transformXLS(req.getRealPath(templateFilename), beans, req.getRealPath(reportFilename));
            req.setAttribute("exportPath", req.getContextPath() + reportFilename);
        }
        return mapping.findForward("download");
    }

}
